//
//  dataoperations.h
//  Draco
//
//  Created by Liu Dafan on 12-2-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#ifndef Draco_dataoperations_h
#define Draco_dataoperations_h

#include "common.h"
#include "connpool.h"

class DataOperations
{
public:
    static void Init(const Config* conf);
    static void UnInit();
    
    static HResult WriteDirectoryInfo(const std::string& tablename, const DIR_INFO& dirinfo);

    static HResult ReadDirectoryInfo(const std::string& tablename, const std::string& rowkey, OUT DIR_INFO& dirinfo);
    
    static HResult ListDirectoryInfo(const std::string& tablename, const std::string& startrow, OUT std::vector<DIR_INFO>& dirs);

    static HResult WriteFileInfo(const std::string& tablename, const FILE_INFO& meta);

    static HResult ReadFileInfo(const std::string& tablename, const std::string& rowkey, OUT FILE_INFO& meta);

    static HResult ListFileInfo(const std::string& tablename, const std::string& startrow, OUT std::vector<FILE_INFO>& files);

    static HResult ListFileVersion(const std::string& tablename, const std::string& startrow, const std::string& filename, OUT std::vector<FILE_INFO>& files);

    static HResult WriteDataBlock(const std::string& filename, const std::vector<int8_t>& block, OUT DataLocation& location);

    static HResult ReadDataBlock(const std::string& filename, uint64_t offset, OUT int8_t* data, size_t length, OUT size_t& actualReadCount);

    static HResult GetNewDirId(USER_ID userid, DIR_ID& dirid);
    
    static HResult GetCurrentFileName(USER_ID userid, std::string& filename);
    
    static std::string  GetNewFileName(USER_ID userid);
    
    static HResult GetFileLength(const std::string& filename, uint64_t& length);

private:
    static ConnPool*    _cp;
    static char         _rootdir[256];
    static const char*  DIRID_FILE;
    static const char*  DATA_FILE_PREFIX;
};

#endif
